<template>
  <div>
    <el-dialog :title="Aid == '' ? '添加排程' : '编辑排程'" :visible.sync="addEditIsDialog_c" @close="resetForm('formName')"
      width="800px">
      <el-form :model="ruleForm" :rules="rules" ref="formName" label-width="120px" class="demo-ruleForm"
        :class="{ 'form-rules-show': seeType }">



        <el-row>
          <el-col :span="12">
            <el-form-item label="任务类型：" prop="type">
              <el-select @change="getIsShow(ruleForm.type)" v-model="ruleForm.type" placeholder="请选择类型" clearable>
                <el-option v-for="item in typeList" :key="item.value" :label="item.label" :value="item.value">
                </el-option>
                <!-- <el-option label="投料" value="0"></el-option>
                <el-option label="换水" value="1"></el-option>
                <el-option label="排水" value="2"></el-option>
                <el-option label="增水" value="3"></el-option> -->
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="排水分组：" prop="dischargeWaterGroupId">
              <el-select @change="getFish" v-model="ruleForm.dischargeWaterGroupId" placeholder="请选择排水分组" clearable>
                <el-option v-for="item in drainageList" :key="item.dischargeWaterGroupId"
                  :label="item.dischargeWaterGroupId" :value="item.dischargeWaterGroupId"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row v-if="isShow">
          <el-col :span="12">
            <el-form-item label="选择鱼池：">
              <el-select v-model="ruleForm.fishpondId" placeholder="请选择鱼池" clearable>
                <el-option v-for="item in fishIdList" :key="item.fishpondId" :label="item.fishpondName"
                  :value="item.fishpondId"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="投喂量：" prop="feedingAmount">
              <el-input v-model="ruleForm.feedingAmount" placeholder="请输入投喂量" clearable show-word-limit
                :disabled="seeType"></el-input>
            </el-form-item>
          </el-col>

        </el-row>
        <el-row v-if="isShow">

          <el-col :span="12">
            <el-form-item label="投喂总量：" prop="feedingTotal">
              <el-input v-model="ruleForm.feedingTotal" placeholder="请输入投喂总量" clearable show-word-limit
                :disabled="seeType"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="投料分组：">
              <el-select v-model="ruleForm.feedingGroup" placeholder="请选择投料分组" clearable>
                <el-option v-for="item in groupList" :key="item.id" :label="item.id" :value="item.value"></el-option>
              </el-select>

            </el-form-item>
          </el-col>
        </el-row>
        <el-row v-if="isShow2">
          <el-col :span="24">
            <el-form-item label="选择水阀：">
              <el-select v-model="ruleForm.valveGroupId" placeholder="请选择水阀" clearable>
                <el-option v-for="item in watervalveList" :key="item.fishpondId" :label="item.valveName"
                  :value="item.valveCroupId"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <!-- <el-col :span="8">
            <el-form-item label="投喂量：" prop="feedingAmount">
              <el-input v-model="ruleForm.feedingAmount" placeholder="请输入投喂量" clearable show-word-limit
                :disabled="seeType"></el-input>
            </el-form-item>
          </el-col> -->

        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="开始时间：">
              <el-time-picker is-range :value-format="'HH:mm'" :format="'HH:mm'" v-model="time" range-separator="至"
                start-placeholder="开始时间" end-placeholder="结束时间" placeholder="选择时间范围">
              </el-time-picker>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="resetForm('formName')">取 消</el-button>
        <el-button class="btn-light-blue" @click="submitForm('formName')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script type="text/ecmascript-6">
import { getObjFishpond, saveObjFishpond } from "@/api/fishpond";
import { getCompanyInfoTree } from "@/api/company";
import { getConfigFishpondTask, getSysCompanyBases, getSysFishTypes, getConfigFishpondValves, saveConfigFishpondTasksByChart, getDischargeWaterGroupIds, getDischargeWaterGroupIdFishponds } from "@/api/system";

export default {
  components: {},
  props: {
    addEditId: {
      type: String,
      default: "",
    },
    addEditStatus: {
      type: String,
      default: "add",
    },
    addEditIsDialog: {
      type: Boolean,
      default: false,
    },
    Aid: {
      type: String,
      default: "",
    },
    baseId: {
      default: "",
    },
    companyId: {
      default: "",

    }
  },
  data() {
    return {
      typeList: [
        { value: 0, label: "投料" },
        { value: 1, label: "换水" },
        { value: 2, label: "排污" },
        { value: 3, label: "增氧" }
      ],
      time: '',
      id: '',
      drainageList: [],
      fishIdList: [],
      watervalveList: [],
      ruleForm: {
        companyId: '',
        type: '',
        // 表单字段
        baseId: '',
        startTime: "",
        endTime: "",
        type: "",
        dischargeWaterGroupId: "",
        fishpondId: ''

      },
      groupList: [],
      rules: {
        // 表单规则
        type: [{ required: true, message: "请选择任务类型", trigger: "blur" }],
        dischargeWaterGroupId: [{ required: true, message: "请输入部门名称", trigger: "blur" }],
        fishpondId: [{ required: true, message: "请选择鱼池", trigger: "blur" }],
        feedingAmount: [{ type: "number", message: "投喂量必须为数字", trigger: "blur" }],
        feedingTotal: [{ type: "number", message: "投喂总量必须为数字", trigger: "blur" }],

      },
      addEditIsDialog_c: false, // 弹窗是否显示
      seeType: false, // 是否禁用
      baseArray: [], // 基地集合
      companyArray: [], // 公司集合
      fishTypeArray: [], // 鱼类集合
      userType: "", // 用户类型（0超级管理员，1公司管理员）
      isShow: false,
      isShow2: false
      // baseId: ''
    };
  },
  mounted() {
    this.getgroupList()
  },
  watch: {


    addEditIsDialog(val) {
      this.addEditIsDialog_c = val;
      if (this.addEditIsDialog_c) {
        // 添加
        if (this.addEditStatus == "add") {
          this.ruleForm.id = -1;
          this.time = ''
          this.ruleForm = {}
          this.ruleForm.modelId = this.addEditId;
          this.seeType = false;
        }
        // 编辑
        if (this.addEditStatus == "edit") {
          this.id = this.Aid
          this.ruleForm.id = this.Aid;
          this.ruleForm.modelId = this.addEditId;
          this.seeType = false;
          this.getDetail();
        }
        // 查看
        if (this.addEditStatus == "see") {
          this.ruleForm.modelId = this.addEditId;
          this.seeType = true;
          this.getDetail();
        }
        this.userType = Number(window.localStorage.getItem("userType"));
        // 获取公司列表
        this.getCompanyInfoTree();

        this.getSysCompanyBases();
        this.getdrainageList()
      }
    },
  },
  methods: {
    getgroupList() {
      for (let i = 1; i < 41; i++) {
        let a = {
          id: i,
          value: i
        }
        this.groupList.push(a)
      }
    },
    getFish(data) {
      let a = {
        baseId: this.baseId,
        dischargeWaterGroupId: data
      }
      getDischargeWaterGroupIdFishponds(a).then((res) => {
        if (res.code == 200) {
          this.fishIdList = res.data;
        }
      });
      let b = {
        baseId: this.baseId,
        id: data
      }
      getConfigFishpondValves(b).then((res) => {
        if (res.code == 200) {
          this.watervalveList = res.data;
        }
      });

    },

    getdrainageList() {
      let id = {
        id: this.baseId
      }

      getDischargeWaterGroupIds(id).then((res) => {
        if (res.code == 200) {
          this.drainageList = res.data;
        }
      });
    },

    getIsShow(a) {
      if (a == 0) {
        this.isShow = true
        this.isShow2 = false;
      } if (a == 1 || a == 2) {
        this.isShow2 = true
        this.isShow = false
      }
      if (a == 3) {
        this.isShow2 = false
        this.isShow = false
      }
      // else {
      //   this.isShow = false
      //   this.isShow2 = false
      // }


    },
    // 详情
    getDetail() {

      getConfigFishpondTask({ id: this.id }).then((res) => {
        if (res.code == 200) {
          this.time = [
            res.data.startTime,
            res.data.endTime
          ]
          this.ruleForm = res.data;


          // 获取公司列表
          this.getCompanyInfoTree();

          this.getSysCompanyBases();

          this.getSysFishTypes();
        }
      });
    },
    // 提交
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.ruleForm.baseId = this.baseId
          this.ruleForm.companyId = this.companyId
          this.ruleForm.startTime = this.time[0]
          this.ruleForm.endTime = this.time[1]
          saveConfigFishpondTasksByChart(this.ruleForm).then((res) => {
            if (res.code == 200) {
              this.$message({
                type: "success",
                message: res.msg,
              });

              // 关闭弹窗
              this.$parent.closeAddEdit();
              // 刷新列表
              this.$parent.getFirstList();
            }
          });

          // this.ruleForm.startTime = String(this.ruleForm.startTime).slice(15, 21)
          // this.ruleForm.endTime = String(this.ruleForm.endTime).slice(15, 21)

        }
      });
    },
    // 取消
    resetForm(formName) {
      this.$refs[formName].resetFields();
      this.$parent.closeAddEdit();
      this.isShow = false
      this.isShow2 = false;
    },
    // 获取公司列表
    getCompanyInfoTree() {
      if (this.ruleForm.userType === "") {
        return;
      }

      if (this.ruleForm.userType == 0) {
        // 超级管理员，公司只有一个总公司
        this.companyArray = [
          {
            id: 0,
            name: "总公司",
            parentId: 0,
          },
        ];
      } else {
        getCompanyInfoTree({}).then((res) => {
          if (res.code == 200) {
            this.companyArray = res.data;
          }
        });
      }
    },
    // 公司下拉切换
    changeTreeItem(val) {
      if (val) {
        this.ruleForm.companyId = val[val.length - 1];
      } else {
        this.ruleForm.companyId = "";
      }

      this.ruleForm.baseId = "";
      this.baseArray = [];

      // 获取基地列表
      this.getSysCompanyBases();

    },
    // 获取基地列表
    getSysCompanyBases() {
      if (this.ruleForm.companyId === "" || this.ruleForm.companyId == null) {
        return;
      }

      getSysCompanyBases({ id: this.ruleForm.companyId }).then((res) => {
        if (res.code == 200) {
          this.baseArray = res.data;

        }
      });


    },
    getSysFishTypes() {
      getSysFishTypes().then((res) => {
        if (res.code == 200) {
          this.fishTypeArray = res.data;
        }
      });
    },
  },
};
</script>

<style lang="less" scoped>
/deep/.el-input__icon {
  font-size: 0;
}

.demo-ruleForm {
  width: 95%;
  margin: 0 auto;
  padding-top: 10px;
}

.el-cascader {
  width: 100%;
}

.el-select {
  width: 100%;
}
</style>
